package com.liangzhong.view
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;

	/**
	 *模块加载条 
	 * @author lake
	 */	
	public class SmalLoadinglProgressBar extends Sprite
	{
		private static var _instance :SmalLoadinglProgressBar;
		
		public var symbolSkin		:MovieClip;//皮肤
		
		private var nameTxt 	:TextField;//加载资源名
		private var percentTxt :TextField;//加载百分比
		/**
		 *背景sp 
		 */		
		private var bgSp 		:Sprite 
		
		public function SmalLoadinglProgressBar()
		{
			initUI();
		}
		/**
		 *获取单例 
		 * @return 
		 * 
		 */		
		public static function get instance():SmalLoadinglProgressBar
		{
			if(_instance == null)
			{
				_instance = new SmalLoadinglProgressBar();
			}
			return _instance;
		}
		/**
		 * 初始化
		 * @param view
		 * 
		 */		
		public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("SmalLoadinglProgressBar");
			if(symbolSkin == null)return;
			symbolSkin.gotoAndStop(0);
			nameTxt = symbolSkin.nameTxt;
			percentTxt = symbolSkin.percentTxt;
			bgSp = new Sprite();
			with(bgSp.graphics)
			{
				beginFill(0x123456,.1);
				drawRect(0, 0, GlobalDef.GameStage.stageWidth, GlobalDef.GameStage.stageHeight);
				endFill();
			}
			this.addChild(bgSp);
			this.addChild(symbolSkin);
			symbolSkin.x = (GlobalDef.GameStage.stageWidth - symbolSkin.width)>>1;
			symbolSkin.y = (GlobalDef.GameStage.stageHeight - symbolSkin.height)>>1;
			addEvtListener();
		}
		/**
		 * 添加事件
		 * 
		 */		
		public function addEvtListener():void
		{
			GlobalDef.GameStage.addEventListener(Event.RESIZE,onResizeHandler);
		}
		/**
		 *舞台大小改变 
		 * @param e
		 * 
		 */		
		private function onResizeHandler(e :Event):void
		{
			symbolSkin.x = (GlobalDef.GameStage.stageWidth - symbolSkin.width)>>1;
			symbolSkin.y = (GlobalDef.GameStage.stageHeight - symbolSkin.height)>>1;
			bgSp.width = GlobalDef.GameStage.stageWidth;
			bgSp.height = GlobalDef.GameStage.stageHeight;
		}
		/**
		 * 删除事件
		 * 
		 */		
		public function removeEvtListener():void
		{
		}
		/**
		 *设置名字 
		 * @param value
		 * 
		 */		
		public function setResName(value :String = ""):void
		{
			nameTxt.htmlText = value;
		}
		/**
		 *设置百分比 
		 * @param value
		 * 
		 */		
		public function setPercent(value :String = "1%"):void
		{
			var tFrameIndex :int = int(value.split("%")[0]);
			var showFrame :int = tFrameIndex * symbolSkin.totalFrames / 100;
			symbolSkin.gotoAndStop(showFrame);
			percentTxt.htmlText = value;
		}
		/**
		 * 销毁
		 * 
		 */		
		public function destroyView():void
		{
			removeEvtListener();
			_instance = null;
			DisplayObjectUtil.removeAllChildren(this);
		}
	}
}